using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using Volo.Abp.Domain.Entities.Auditing;

namespace CKY.AgentPlatform.Users
{
    /// <summary>
    /// 角色实体
    /// </summary>
    public class Role : FullAuditedAggregateRoot<Guid>
    {
        /// <summary>
        /// 角色名称
        /// </summary>
        public virtual string Name { get; set; }

        /// <summary>
        /// 角色显示名称
        /// </summary>
        public virtual string DisplayName { get; set; }

        /// <summary>
        /// 角色描述
        /// </summary>
        public virtual string Description { get; set; }

        /// <summary>
        /// 是否内置角色
        /// </summary>
        public virtual bool IsBuiltIn { get; set; }

        /// <summary>
        /// 是否默认角色
        /// </summary>
        public virtual bool IsDefault { get; set; }

        /// <summary>
        /// 角色优先级（数字越小优先级越高）
        /// </summary>
        public virtual int Priority { get; set; }

        /// <summary>
        /// 角色权限列表
        /// </summary>
        public virtual List<string> Permissions { get; set; } = new List<string>();

        protected Role()
        {
            // EF Core required constructor
        }

        public Role(
            Guid id,
            [NotNull] string name,
            [NotNull] string displayName,
            string description = null,
            bool isBuiltIn = false,
            bool isDefault = false,
            int priority = 0)
            : base(id)
        {
            Name = name;
            DisplayName = displayName;
            Description = description;
            IsBuiltIn = isBuiltIn;
            IsDefault = isDefault;
            Priority = priority;
        }

        /// <summary>
        /// 添加权限
        /// </summary>
        public virtual void AddPermission(string permission)
        {
            if (!Permissions.Contains(permission))
            {
                Permissions.Add(permission);
            }
        }

        /// <summary>
        /// 移除权限
        /// </summary>
        public virtual void RemovePermission(string permission)
        {
            Permissions.Remove(permission);
        }

        /// <summary>
        /// 检查是否有指定权限
        /// </summary>
        public virtual bool HasPermission(string permission)
        {
            return Permissions.Contains(permission);
        }

        /// <summary>
        /// 批量设置权限
        /// </summary>
        public virtual void SetPermissions(IEnumerable<string> permissions)
        {
            Permissions.Clear();
            Permissions.AddRange(permissions);
        }

        /// <summary>
        /// 设置默认角色状态
        /// </summary>
        public virtual void SetDefault(bool isDefault)
        {
            IsDefault = isDefault;
        }

        /// <summary>
        /// 设置优先级
        /// </summary>
        public virtual void SetPriority(int priority)
        {
            Priority = priority;
        }
    }
}